package com.example.layui.demo.controller;

import com.example.layui.demo.entity.Article;
import com.example.layui.demo.entity.ArticleVO;
import com.example.layui.demo.entity.ResultEntity;
import com.example.layui.demo.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的测试
 */
@Controller
@RequestMapping("/test")
public class TestController {
    @Autowired
    MyService myService;

    @GetMapping("/getDetailbyId/{id}")
    @ResponseBody
    public Article getDetailbyId(@PathVariable("id") Integer id) {
        Article Article = myService.getArticleByid(id);
        return Article;
    }

    @GetMapping("/list")
    public String gotoListPage() {
        return "listpage";
    }


//    @GetMapping("/getList")
//    @ResponseBody
//    public ResultEntity<List<Article>> getList(ArticleVO articlevo) {
//        List<Article> list = myService.getArticleList1(articlevo);
//        Integer articleCountWhere = myService.getArticleCountWhere(articlevo);
//        ResultEntity resultEntity =null;
//        if (list.size() > 0) {
//            resultEntity = new ResultEntity(0, "成功", list, articleCountWhere);
//        }
//        return resultEntity;
//    }

    @PostMapping("/getAticleByid/{id}")
    @ResponseBody
    public Map<String, Object> getAticleByID(@PathVariable("id") int id) {


        Map<String, Object> map = new HashMap<>();
        Article article = myService.getArticleByid(id);

//        "code": 0,
////                "msg": "...",
////                "count": 3,

        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", 1);//总记录数
        map.put("data", article);
        return map;

    }

    @GetMapping("/delArticleById/{id}")
    @ResponseBody
    public Map<String, Object> delArticleById(@PathVariable("id") Integer id) {
        Map<String, Integer> paramMap = new HashMap<>();

        Integer del = myService.delArticlebyId(id);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "删除");
        result.put("data", del);
        return result;

    }

    @PostMapping("/updateArticle")
    @ResponseBody
    public Map<String, Object> updateArticleById(Article obj) {


        Integer updateArticle = myService.updateArticle1(obj);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "更新");
        result.put("data", updateArticle);
        return result;

    }

    @PostMapping("/deleteByIds")
    @ResponseBody
    public Map<String, Object> deleteByIds(String[] arr) {
        int[] ids = new int[arr.length];
        for (int i=0 ;i<arr.length;i++) {
            ids[i]= Integer.parseInt(arr[i]);
        }

        Integer updateArticle = myService.deleteByIds(ids);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "刪除");
        result.put("data", updateArticle);
        return result;

    }

//    @PostMapping("/search")
//    @ResponseBody
//    public ResultEntity<List<Article>> search(ArticleVO articlevo) {
//        List<Article> list = myService.getArticleList1(articlevo);
//        Integer articleCountWhere = myService.getArticleCountWhere(articlevo);
//        ResultEntity resultEntity =null;
//        if (list.size() > 0) {
//             resultEntity = new ResultEntity(0, "成功", list, articleCountWhere);
//        }
//            return resultEntity;
//        }



}
